package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.web.domain.Product;
import com.ruoyi.web.domain.RefundRecord;
import com.ruoyi.web.mapper.RefundRecordMapper;
import com.ruoyi.web.service.IRefundRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 退款Service业务层处理
 * 
 * @author song
 * @date 2024-05-17
 */
@Service
public class RefundRecordServiceImpl implements IRefundRecordService
{
    @Autowired
    private RefundRecordMapper refundRecordMapper;


    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    /**
     * 查询退款
     * 
     * @param id 退款主键
     * @return 退款
     */
    @Override
    public RefundRecord selectRefundRecordById(Long id)
    {
        return refundRecordMapper.selectRefundRecordById(id);
    }

    /**
     * 查询退款列表
     * 
     * @param refundRecord 退款
     * @return 退款
     */
    @Override
    public List<RefundRecord> selectRefundRecordList(RefundRecord refundRecord)
    {
        return refundRecordMapper.selectRefundRecordList(refundRecord);
    }

    /**
     * 新增退款
     * 
     * @param refundRecord 退款
     * @return 结果
     */
    @Override
    public int insertRefundRecord(RefundRecord refundRecord)
    {
        refundRecord.setCreateTime(DateUtils.getNowDate());
        return refundRecordMapper.insertRefundRecord(refundRecord);
    }

    /**
     * 修改退款
     * 
     * @param refundRecord 退款
     * @return 结果
     */
    @Override
    public int updateRefundRecord(RefundRecord refundRecord)
    {
        refundRecord.setUpdateTime(DateUtils.getNowDate());
        return refundRecordMapper.updateRefundRecord(refundRecord);
    }

    /**
     * 批量删除退款
     * 
     * @param ids 需要删除的退款主键
     * @return 结果
     */
    @Override
    public int deleteRefundRecordByIds(Long[] ids)
    {
        return refundRecordMapper.deleteRefundRecordByIds(ids);
    }

    /**
     * 删除退款信息
     * 
     * @param id 退款主键
     * @return 结果
     */
    @Override
    public int deleteRefundRecordById(Long id)
    {
        return refundRecordMapper.deleteRefundRecordById(id);
    }

    /**
     * 导入用户数据
     *
     * @param refundRecords 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importData(List<RefundRecord> refundRecords, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(refundRecords) || refundRecords.isEmpty())
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RefundRecord refundRecord : refundRecords)
        {
            try
            {
                // 验证是商品号是否存在
                RefundRecord p = refundRecordMapper.selectRefundRecordByProductNo(refundRecord.getProductNo());

                if (StringUtils.isNull(p))
                {

                    refundRecord.setCreateBy(operName);
                    refundRecordMapper.insertRefundRecord(refundRecord);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、产品 " + refundRecord.getProductNo() + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、产品 " + refundRecord.getProductNo() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、产品 " + refundRecord.getProductNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
